package com.palrevo.data.services;

import java.util.HashMap;
import java.util.Map;

import com.palrevo.data.dao.DAO;
import com.palrevo.data.dao.DAOFactory;
import com.palrevo.data.dao.DaoRecord;
import com.palrevo.data.dao.DataException;
import com.palrevo.data.models.AdverAttachment;
import com.palrevo.data.models.Advertisement;
import com.palrevo.data.services.ServiceBase.Columns;
import com.palrevo.data.services.ServiceBase.Tables;
import com.palrevo.utils.Log;

/**
 * 
 * @author AAsha
 * 
 */
public class AdvertisementDBService {

	/**
	 * 
	 */
	private AdvertisementDBService() {
	}

	/**
	 * 
	 * @author AAsha
	 * 
	 */
	private static class SingletonHolder {
		private static AdvertisementDBService instance = new AdvertisementDBService();
	}

	/**
	 * 
	 * @return AdvertisementDBService instance
	 */
	public static AdvertisementDBService getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 
	 * @return
	 * @throws DataException
	 */
	public Map<Long, Advertisement> loadMainAdver() throws DataException {
		return loadAdver("M");
	}
	
	/**
	 * 
	 * @return
	 * @throws DataException
	 */
	public Map<Long, Advertisement> loadSideAdver() throws DataException {
		return loadAdver("S");
	}
	
	/**
	 * 
	 * @return
	 * @throws DataException
	 */
	public Map<Long, Advertisement> loadSponsorAdver() throws DataException {
		return loadAdver("SP");
	}

	/**
	 * 
	 * @return list of site main advers
	 * @throws DataException
	 */
	public Map<Long, Advertisement> loadAdver(String type) throws DataException {

		Advertisement advr = null;
		Map<Long, Advertisement> advers = null;
		AdverAttachment adverAttachment = null;

		try {

			DAO dao1 = DAOFactory.getInstance().createDao();

			String sql1 = "SELECT * FROM " + Tables.advertisements.name()
					+ " WHERE " + Columns.TYPE.name() + " = '" + type
					+ "' ORDER BY " + Columns.INDX.name();
			dao1.execute(sql1);

			advers = new HashMap<Long, Advertisement>();

			for (DaoRecord record1 : dao1.getResult()) {
				advr = new Advertisement();
				advr.setId((Long) record1.getValue(Columns.ID.name()));
				advr.setTitle(record1.getValue(Columns.TITLE.name()).toString());
				advr.setBody(record1.getValue(Columns.BODY.name()).toString());
				advr.setIndx(Integer.valueOf(record1.getValue(
						Columns.INDX.name()).toString()));
				advr.setType(record1.getValue(Columns.TYPE.name()).toString());

				DAO dao2 = DAOFactory.getInstance().createDao();
				String sql2 = "SELECT * FROM "
						+ Tables.adver_attachments.name() + " WHERE "
						+ Columns.ADVR_ID.name() + " = " + advr.getId();
				dao2.execute(sql2);

				for (DaoRecord record2 : dao2.getResult()) {
					adverAttachment = new AdverAttachment();
					adverAttachment.setId((Long) record2.getValue(Columns.ID
							.name()));
					adverAttachment.setIcon(record2.getValue(
							Columns.ICON.name()).toString());
					adverAttachment.setSmallIcon(record2.getValue(
							Columns.SMALLICON.name()).toString());
					adverAttachment.setPdf(record2.getValue(Columns.PDF.name())
							.toString());
					advr.getAdverAttchments().add(adverAttachment);
				}

				advers.put(advr.getId(), advr);
			}

		} catch (Exception ex) {
			Log.error(this, ex.getMessage());
			throw new DataException(ex.getMessage(), ex);
		}

		return advers;
	}

	
}
